package com.quanquan.user.controller;

import DTO.PageDTO;
import com.quanquan.user.dto.SelfHelpIntegralDTO;
import com.quanquan.user.entity.PosOrder;
import com.quanquan.user.entity.User;
import com.quanquan.user.exception.SelfHelpIntegralException;
import com.quanquan.user.exception.UserException;
import com.quanquan.user.repository.UserRepository;
import com.quanquan.user.request.PosOrderRequestBody;
import com.quanquan.user.request.SelfHelpRequestBody;
import com.quanquan.user.service.PosOrderService;
import com.quanquan.user.service.SelfHelpIntegralService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import utils.StringUtil;
import web.response.ResultBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:自助积分管理
 *
 * @ClassName: SelfHelpIntegralController
 * @Author: xiaoqianh丶
 * @Date: 2019-04-12 09:55
 * @Version: V1.0
 */
@Controller
@RequestMapping("/self_help_integral")
public class SelfHelpIntegralController {

    private final SelfHelpIntegralService selfHelpIntegralService;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PosOrderService posOrderService;

    @Autowired
    public SelfHelpIntegralController(SelfHelpIntegralService selfHelpIntegralService) {
        this.selfHelpIntegralService = selfHelpIntegralService;
    }

    /**
     * 创建后端使用
     * @param requestBody
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultBody createApplication (@RequestBody SelfHelpRequestBody requestBody)  {

        if(StringUtil.isNotEmpty(requestBody.getStoreId())&&!(requestBody.getStoreId()).equals("null")){
            boolean msg = false;
            try {
                msg = selfHelpIntegralService.createSelfHelpApplicationAndAuto(requestBody);
            } catch (SelfHelpIntegralException e) {
                e.printStackTrace();
                return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg(), msg);
            }
            if(!msg){
                return new ResultBody(ResCodeEnum.APPLICATION_FAIL.getCode(), ResCodeEnum.APPLICATION_FAIL.getMsg(), msg);
            }
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), msg);
        }
        return new ResultBody(ResCodeEnum.STOREID_NOT.getCode(), ResCodeEnum.STOREID_NOT.getMsg());

    }


    /**
     * 自助小票待审核列表
     * @param requestBody
     * @return
     */
    @PostMapping("/check_list_jf")
    @ResponseBody
    public ResultBody checkListJF (@RequestBody SelfHelpRequestBody requestBody){
        PageDTO<SelfHelpIntegralDTO> selfHelpIntegralPageDTO = null;
        String applicationStatus ="reviewing";
        selfHelpIntegralPageDTO = selfHelpIntegralService.listJF(requestBody,applicationStatus);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), selfHelpIntegralPageDTO);
    }
    /**
     * 自助小票审核通过列表
     * @param requestBody
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public ResultBody list (@RequestBody SelfHelpRequestBody requestBody){
        PageDTO<SelfHelpIntegralDTO> selfHelpIntegralPageDTO = null;
        String applicationStatus ="successpass";
        selfHelpIntegralPageDTO = selfHelpIntegralService.list(requestBody,applicationStatus);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), selfHelpIntegralPageDTO);
    }


    /**
     * 自助小票待审核列表
     * @param requestBody
     * @return
     */
    @PostMapping("/check_list")
    @ResponseBody
    public ResultBody checkList (@RequestBody SelfHelpRequestBody requestBody){
        PageDTO<SelfHelpIntegralDTO> selfHelpIntegralPageDTO = null;
        String applicationStatus ="reviewing";
        selfHelpIntegralPageDTO = selfHelpIntegralService.list(requestBody,applicationStatus);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), selfHelpIntegralPageDTO);
    }

    /**
     * 自助小票审核记录列表
     * @param requestBody
     * @return
     */
    @PostMapping("/review_result_list")
    @ResponseBody
    public ResultBody reviewResultList (@RequestBody SelfHelpRequestBody requestBody){
        PageDTO<SelfHelpIntegralDTO> selfHelpIntegralPageDTO = null;
        String applicationStatus ="failpass";
        selfHelpIntegralPageDTO = selfHelpIntegralService.list(requestBody,applicationStatus);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), selfHelpIntegralPageDTO);
    }

    /**
     * 创建前端使用
     * @param requestBody
     * @return
     */
    @PostMapping("/create")
    @ResponseBody
    public ResultBody create (@RequestBody SelfHelpRequestBody requestBody){
//        String ticketNum = requestBody.getTicketNum();
//        String consumeDay = requestBody.getConsumeDay();
//        String storeId = requestBody.getStoreId();

//        if(StringUtil.isEmpty(ticketNum)){
//            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg(), "小票号缺失");
//        }
//        if(StringUtil.isEmpty(consumeDay)){
//            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg(), "消费日期缺失");
//        }
        String imgUrl = requestBody.getImgUrl();
        if(StringUtil.isEmpty(imgUrl)){
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "小票上传失败");

        }
        String substring = imgUrl.substring(0, 4);
        if(!substring.equals("http")){
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "小票上传失败");
        }
        String msg = selfHelpIntegralService.createSelfHelpApplication(requestBody);

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
    }



    /**
     * 前端使用 （只上传图片)
     * @param requestBody
     * @return
     */
    @PostMapping("/query_list_jf")
    @ResponseBody
    public ResultBody findAllPersonSelfHelpIntegrals(@RequestBody SelfHelpRequestBody requestBody){
        PageDTO<SelfHelpIntegralDTO> selfHelpIntegralDTOPageDTO = selfHelpIntegralService.queryReviewingRecord(requestBody);
        List<SelfHelpIntegralDTO> list = selfHelpIntegralDTOPageDTO.getList();
        System.out.println("selfHelpIntegralDTOPageDTO........"+selfHelpIntegralDTOPageDTO);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), list);

    }

    /**
     * 前端使用
     * @param requestBody
     * @return
     */
    @PostMapping("/query_list")
    @ResponseBody
    public ResultBody  findAllSelfHelpIntegrals(@RequestBody SelfHelpRequestBody requestBody){
        List<SelfHelpIntegralDTO> selfHelpIntegrals = null;
        try {
            selfHelpIntegrals = selfHelpIntegralService.queryAllByStatus(requestBody);
        } catch (SelfHelpIntegralException e) {
            e.printStackTrace();
        }

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), selfHelpIntegrals);
    }


    @PostMapping("/edit")
    @ResponseBody
    public ResultBody querySelfHelpIntegralDetails(@RequestBody SelfHelpRequestBody requestBody){
        String selfHelpIntegralId = requestBody.getSelfHelpIntegralId();
        System.out.println("selfHelpIntegralId...."+selfHelpIntegralId);
        SelfHelpIntegralDTO selfHelpIntegralDetails = selfHelpIntegralService.findSelfHelpIntegralDetailsJF(selfHelpIntegralId);

        if(selfHelpIntegralDetails==null){
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg(), null);

        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), selfHelpIntegralDetails);
    }


    @PostMapping("/review")
    @ResponseBody
    public ResultBody  reviewApplication(@RequestBody SelfHelpRequestBody requestBody){
        String applicationStatus = requestBody.getApplicationStatus();
        String remarkApplication = requestBody.getRemarkApplication();
        //如果没填写就后台来添加
        if(StringUtil.isEmpty(remarkApplication)){
            if(applicationStatus.equals("successpass")){
                remarkApplication = "后台审核通过";
            }else{
                remarkApplication = "后台审核未通过";
            }
        }
        boolean msg = false;
        try {
            msg = selfHelpIntegralService.reviewApplicationA(requestBody);
        } catch (UserException e) {
            e.printStackTrace();
        }
        if(!msg){
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg(), msg);
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), msg);
    }


    @PostMapping("/remove")
    @ResponseBody
    public ResultBody removeApplication (@RequestBody SelfHelpRequestBody requestBody){
        String selfHelpIntegralId = requestBody.getSelfHelpIntegralId();
        boolean msg = selfHelpIntegralService.removeApplication(selfHelpIntegralId);
        if(!msg){
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg(), msg);
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), msg);
    }


    /**
     * 数采扫码核销
     * @param requestBody
     * @return
     */
    @PostMapping("/digital_add")
    @ResponseBody
    public ResultBody digitalSelfHeipIntegral (@RequestBody PosOrderRequestBody requestBody){
        //判断是支付，还是退款
        String applicationStatus = requestBody.getApplicationStatus();
        String ticketNum = requestBody.getTicketNum();
        String userId1 = requestBody.getUserId();

        if(applicationStatus.equals("refund")){
            PosOrder posOrder = posOrderService.findPosOrder(ticketNum);
            String receivePoint = posOrder.getReceivePoint();
            String userId = posOrder.getUserId();
            Map refund = posOrderService.refund(userId, receivePoint, ticketNum);

            if(refund==null){
                return new ResultBody(ResCodeEnum.FAIL.getCode(), "未查询到该用户");
            }
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), refund);
        }
        //根据订单号查询是否有记录
           //先记录交易订单
        PosOrder posOrder = posOrderService.createPosOrder(requestBody);
        Map<String, String> map = selfHelpIntegralService.createSelfHelpApplicationAndAutoAutomatic(posOrder);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), map);

    }
}
